Imports System
Imports System.Text
Imports System.Data
Imports System.Collections.Generic
Imports System.Web
Imports System.IO

Imports Framework

'Collection Class (Customisable half)
Partial Public Class CContentList

#Region "Filters"
	Public Function Search(templateId As Integer, setId As Integer, s As String) As CContentList
		Dim temp As CContentList = Me
		If templateId > 0 Then temp = temp.GetByTemplateId(templateId)
		If setId > 0 Then temp = temp.GetBySetId(setId)
		Return temp.Search(s)
	End Function
	Public Function Search(s As String) As CContentList
		If String.IsNullOrEmpty(s) Then Return Me

		Dim temp As New CContentList
		For Each i As CContent In Me
			If i.ContentText.ToLower.Contains(s) Then temp.Add(i)
		Next
		Return temp
	End Function
#End Region

#Region "Aggregation"
#End Region


#Region "Members"
	Private m_templateSets As CTemplateSetList
	Private m_templateSetIds As List(Of Integer)

	Private m_templates As CTemplateList
	Private m_templateIds As List(Of Integer)
#End Region

#Region "Resolve Associative table (and sort)"
	Public ReadOnly Property [TemplateSets]() As CTemplateSetList
		Get
			If m_templateSets Is Nothing Then
				SyncLock (Me)
					If m_templateSets Is Nothing Then
						m_templateSets = New CTemplateSetList(Me.Count)
						For Each i As CContent In Me
							If IsNothing(i.TemplateSet) Then Continue For
							m_templateSets.Add(i.TemplateSet)
						Next
						m_templateSets.Sort()
					End If
				End SyncLock
			End If
			Return m_templateSets
		End Get
	End Property
	Public Function RemainingTemplateSets(ByVal search As String) As CTemplateSetList
		Dim temp As New CTemplateSetList(CTemplateSet.Cache.Search(search))
		temp.Remove(Me.TemplateSets)
		Return temp
	End Function


	Public ReadOnly Property [Templates]() As CTemplateList
		Get
			If m_templates Is Nothing Then
				SyncLock (Me)
					If m_templates Is Nothing Then
						m_templates = New CTemplateList(Me.Count)
						For Each i As CContent In Me
							If IsNothing(i.Template) Then Continue For
							m_templates.Add(i.Template)
						Next
						m_templates.Sort()
					End If
				End SyncLock
			End If
			Return m_templates
		End Get
	End Property
	Public Function RemainingTemplates(ByVal search As String) As CTemplateList
		Dim temp As New CTemplateList(CTemplate.Cache.Search(search))
		temp.Remove(Me.Templates)
		Return temp
	End Function
#End Region

#Region "Resolve/Isolate PKs"
	Public ReadOnly Property TemplateSetIds() As List(Of Integer)
		Get
			If IsNothing(m_templateSetIds) Then
				SyncLock (Me)
					If IsNothing(m_templateSetIds) Then
						m_templateSetIds = New List(Of Integer)(Me.Count)
						For Each i As CContent In Me
							m_templateSetIds.Add(i.ContentSetId)
						Next
					End If
				End SyncLock
			End If
			Return m_templateSetIds
		End Get
	End Property
	Public ReadOnly Property TemplateIds() As List(Of Integer)
		Get
			If IsNothing(m_templateIds) Then
				SyncLock (Me)
					If IsNothing(m_templateIds) Then
						m_templateIds = New List(Of Integer)(Me.Count)
						For Each i As CContent In Me
							m_templateIds.Add(i.ContentTemplateId)
						Next
					End If
				End SyncLock
			End If
			Return m_templateIds
		End Get
	End Property
#End Region



#Region "Cloning"
	Public Function Clone(ByVal target As CDataSrc) As CContentList ', parentId As Integer) As CContentList
        'No Transaction
        If TypeOf target Is CDataSrcRemote Then Return Clone(target, Nothing) ', parentId)

        'Transaction
        Dim cn As IDbConnection = target.Local.Connection()
        Dim tx As IDbTransaction = cn.BeginTransaction()
        Try
            Clone = Clone(target, tx) ', parentId)
            tx.Commit()
        Catch
            tx.Rollback()
            Throw
        Finally
            cn.Close()
        End Try
    End Function
    Public Function Clone(target as CDataSrc, txOrNull as IDbTransaction) As CContentList ', parentId As Integer) As CContentList
        Dim list As New CContentList(Me.Count)
        For Each i As CContent In Me
            list.Add(i.Clone(target, txOrNull))  ', parentId))    *Child entities must reference the new parent
        Next
        Return list
    End Function
#End Region

#Region "Export to Csv"
    'Web - Need to add a project reference to System.Web, or comment out these two methods
    Public Sub ExportToCsv(ByVal response As HttpResponse)
        ExportToCsv(response, "Contents.csv")
    End Sub
    Public Sub ExportToCsv(ByVal response As HttpResponse, ByVal fileName As String)
        CDataSrc.ExportToCsv(response, fileName) 'Standard response headers
        Dim sw As New StreamWriter(response.OutputStream)
        ExportToCsv(sw)
        sw.Flush()
        response.End()
    End Sub

    'Non-web
    Public Sub ExportToCsv(ByVal filePath As String)
        Dim sw As New StreamWriter(filePath)
        ExportToCsv(sw)
        sw.Close()
    End Sub

    'Logic
    Protected Sub ExportToCsv(ByVal sw As StreamWriter)
        Dim headings As String() = New String() {"ContentSetId", "ContentTemplateId", "ContentText", "ContentCreated", "ContentModified"}
        CDataSrc.ExportToCsv(headings, sw)
        For Each i As CContent In Me
            Dim data As Object() = New Object() {i.ContentSetId, i.ContentTemplateId, i.ContentText, i.ContentCreated, i.ContentModified}
            CDataSrc.ExportToCsv(data, sw)
        Next
    End Sub
#End Region

End Class
